<template>
  <BoxWithTitle class="right-wrapper" title="配置工具" icon="&#xe6f2;">
    <!-- 工具栏 -->
    <div class="tool-box" v-if="!isSet">
      <div
        v-for="tool in tools"
        :key="tool.id"
        :class="['tool-item', { 'current-tool': selectDrawType === tool.id }]"
        @click="handleToolClick(tool.id)"
      >
        <i class="iconfont" :class="tool.iconClass" v-html="tool.icon"></i>
        <p>{{ tool.name }}</p>
      </div>
    </div>

    <div class="tool-box" v-if="isSet">
      <div
        v-for="tool in toolsSet"
        :key="tool.id"
        :class="['tool-item', { 'current-tool': selectDrawType === tool.id }]"
        @click="handleToolClickSet(tool.id)"
      >
        <i class="iconfont" :class="tool.iconClass" v-html="tool.icon"></i>
        <p>{{ tool.name }}</p>
      </div>
    </div>

    <div v-if="isSet" class="drawType">
      <div class="title">
        <i class="el-icon-data-analysis"></i>
        <span>绘制类型</span>
      </div>
      <div class="draw-type-content">
        <el-radio-group v-model="localDrawType" @change="handleChangeDrawImageType" class="custom-radio-group">
          <el-radio :label="1" class="custom-radio">
            <div class="radio-content">
              <i class="iconfont">&#xe6ab;</i>
              <span>变位图</span>
            </div>
          </el-radio>
          <el-radio :label="2" class="custom-radio">
            <div class="radio-content">
              <i class="iconfont">&#xe708;</i>
              <span>单矢量图</span>
            </div>
          </el-radio>
          <el-radio :label="3" class="custom-radio">
            <div class="radio-content">
              <i class="iconfont">&#xe75e;</i>
              <span>合矢量图</span>
            </div>
          </el-radio>
        </el-radio-group>
        
        <div class="type-description">
          <div v-if="localDrawType === 1" class="description-item">
            <i class="el-icon-info"></i>
            <span>变位图：显示测点的位置变化情况</span>
          </div>
          <div v-else-if="localDrawType === 2" class="description-item">
            <i class="el-icon-info"></i>
            <span>单矢量图：显示单个测点的矢量数据</span>
          </div>
          <div v-else-if="localDrawType === 3" class="description-item">
            <i class="el-icon-info"></i>
            <span>合矢量图：显示多个测点的矢量数据对比</span>
          </div>
        </div>
      </div>
    </div>
    
    <el-collapse v-model="currentTool" accordion v-if="!isSet" class="custom-collapse-container">
      <!-- 测点集合 -->
      <el-collapse-item name="1" class="custom-collapse-item">
        <template slot="title">
          <div class="collapse-title-wrapper">
            <i class="el-icon-location-information title-icon"></i>
            <span class="collapse-title">测点集合</span>
            <el-badge :value="allElementPoint.length" :max="99" class="badge-count" type="primary" />
          </div>
        </template>
        
        <div v-if="allElementPoint.length > 0" class="element-list-wrapper">
          <div
            v-for="item in allElementPoint"
            :key="item.id"
            class="element-item point-item"
          >
            <div class="element-main">
              <div class="element-marker" :style="{ backgroundColor: item.color }">
                <i class="el-icon-position"></i>
              </div>
              <div class="element-info">
                <div class="element-name">{{ item.pointName }}</div>
                <div class="element-id" v-if="item.code_ID">ID: {{ item.code_ID }}</div>
              </div>
            </div>
            <div class="element-actions">
              <el-tooltip effect="dark" content="修改测点" placement="top">
                <el-button 
                  type="primary" 
                  size="mini" 
                  circle
                  class="action-btn edit-btn"
                  @click="handleEditPoint(item)"
                >
                  <i class="el-icon-edit"></i>
                </el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除测点" placement="top">
                <el-button
                  type="danger"
                  size="mini"
                  circle
                  class="action-btn delete-btn"
                  @click="handleDelPoint(item)"
                >
                  <i class="el-icon-delete"></i>
                </el-button>
              </el-tooltip>
            </div>
          </div>
        </div>
        <div v-else class="empty-state">
          <i class="el-icon-location-outline empty-icon"></i>
          <p class="empty-text">暂无测点数据</p>
          <span class="empty-tip">请先添加测点到图中</span>
        </div>
      </el-collapse-item>

      <!-- 文字集合 -->
      <el-collapse-item name="2" class="custom-collapse-item">
        <template slot="title">
          <div class="collapse-title-wrapper">
            <i class="el-icon-document title-icon"></i>
            <span class="collapse-title">文字集合</span>
            <el-badge :value="allElementText.length" :max="99" class="badge-count" type="success" />
          </div>
        </template>
        
        <div v-if="allElementText.length > 0" class="element-list-wrapper">
          <div
            v-for="item in allElementText"
            :key="item.ID"
            class="element-item text-item"
          >
            <div class="element-main">
              <div class="element-marker text-marker">
                <i class="el-icon-document"></i>
              </div>
              <div class="element-info">
                <div class="element-name">{{ item.text }}</div>
                <div class="element-id" v-if="item.fontSize">字号: {{ item.fontSize }}px</div>
              </div>
            </div>
            <div class="element-actions">
              <el-tooltip effect="dark" content="修改文字" placement="top">
                <el-button 
                  type="primary" 
                  size="mini" 
                  circle
                  class="action-btn edit-btn"
                  @click="handleEditText(item)"
                >
                  <i class="el-icon-edit"></i>
                </el-button>
              </el-tooltip>
              <el-tooltip effect="dark" content="删除文字" placement="top">
                <el-button
                  type="danger"
                  size="mini"
                  circle
                  class="action-btn delete-btn"
                  @click="handleDelText(item)"
                >
                  <i class="el-icon-delete"></i>
                </el-button>
              </el-tooltip>
            </div>
          </div>
        </div>
        <div v-else class="empty-state">
          <i class="el-icon-document-delete empty-icon"></i>
          <p class="empty-text">暂无文字数据</p>
          <span class="empty-tip">请先添加文字到图中</span>
        </div>
      </el-collapse-item>
    </el-collapse>
  </BoxWithTitle>
</template>

<script>
import BoxWithTitle from '@/components/base/BoxWithTitle.vue'

export default {
  name: 'ToolPanel',
  components: {
    BoxWithTitle
  },
  props: {
    selectDrawType: {
      type: [String, Number],
      default: ''
    },
    allElementPoint: {
      type: Array,
      default: () => []
    },
    allElementText: {
      type: Array,
      default: () => []
    },
    isSet:{
      type:Boolean,
      default:false
    },
    drawType:{
      type:Number,
      default:1
    }
  },
  data() {
    return {
      currentTool: ['1'],
      tools: [
        { id: 1, name: '测点定位', icon: '&#xe755;', iconClass: 'blue-text'},
        { id: 2, name: '比例尺', icon: '&#xe76d;', iconClass: 'green-text'},
        { id: 6, name: '添加文字', icon: '&#xe647;', iconClass: 'purple-text'},
        
      ],
      toolsSet:[
        { id: 3, name: '测点关联', icon: '&#xe61c;', iconClass: 'blue-text'},
        { id: 4, name: '方向设置', icon: '&#xe652;', iconClass: 'green-text'},
        { id: 5, name: '绘制设置', icon: '&#xe75e;', iconClass: 'purple-text'}
      ],
      localDrawType: this.drawType
    }
  },
  watch: {
    drawType(newVal) {
      this.localDrawType = newVal
    }
  },
  methods: {
    handleToolClick(type) {
      this.$emit('change-draw-type', this.selectDrawType === type ? 0 : type)
    },
    handleToolClickSet(type){
      this.$emit('change-draw-type', type)
    },
    handleEditPoint(item) {
      this.$emit('edit-point', item)
    },
    handleDelPoint(item) {
      this.$emit('del-point', item)
    },
    handleEditText(item) {
      this.$emit('edit-text', item)
    },
    handleDelText(item) {
      this.$emit('del-text', item)
    },
    handleChangeDrawImageType(val){
      this.localDrawType = val
      this.$emit('draw-image-type', val)
    },
  },
}
</script>

<style scoped lang="scss">
.right-wrapper {
  width: 340px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  
  .tool-box {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 12px;
    padding: 16px;
    
    .tool-item {
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 12px 0;
      border: 1px solid #ebeef5;
      border-radius: 6px;
      cursor: pointer;
      transition: all 0.3s ease;
      
      &:hover {
        border-color: #409eff;
        box-shadow: 0 2px 12px 0 rgba(32, 160, 255, 0.1);
        transform: translateY(-2px);
      }
      
      i {
        font-size: 28px;
        margin-bottom: 8px;
        
        &.blue-text {
          color: #409eff;
        }
        &.green-text {
          color: #67c23a;
        }
        &.purple-text {
          color: #9254de;
        }
      }
      
      p {
        font-size: 12px;
        color: #606266;
        margin: 0;
      }
    }
    
    .current-tool {
      border-color: #409eff;
      background-color: #ecf5ff;
      box-shadow: 0 0 8px 0 rgba(32, 160, 255, 0.2);
    }
  }
}

::v-deep .el-collapse {
  border: none;
  
  .el-collapse-item {
    
    &:last-child {
      border-bottom: none;
    }
    
    &__header {
      height: 48px;
      line-height: 48px;
      font-size: 13px;
      font-weight: 600;
      color: #303133;
      padding: 0 8px;
      
      &:hover {
        color: #409eff;
      }
    }
    
    &__wrap {
      border: none;
    }
    
    &__content {
      padding-bottom: 0;
    }
  }
}

.point-list-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding:5px 12px;
  border-radius: 4px;
  margin-bottom: 8px;
  transition: background-color 0.3s;
  
  &:hover {
    background-color: #f5f7fa;
  }
  
  .point-info, .text-info {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0;
  }
  
  .point-marker {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    margin-right: 10px;
    flex-shrink: 0;
  }
  
  .text-marker {
    width: 12px;
    height: 12px;
    border-radius: 2px;
    margin-right: 10px;
    flex-shrink: 0;
    background-color: #909399;
  }
  
  .point-name, .text-content {
    font-size: 13px;
    color: #606266;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  .point-actions, .text-actions {
    display: flex;
    gap: 8px;
  }
}

.empty-tips {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: #909399;
  
  i {
    font-size: 32px;
    margin-bottom: 12px;
  }
  
  p {
    font-size: 14px;
    margin: 0;
  }
}
::v-deep .el-button--mini{
  padding:5px !important
}
.point-list-wrapper{
  max-height: 400px;
  overflow: auto;
}
.drawType {
  padding: 16px;
  box-sizing: border-box;
  background: linear-gradient(135deg, #f8faff 0%, #f0f4ff 100%);
  border-radius: 8px;
  margin: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  
  .title {
    display: flex;
    align-items: center;
    line-height: 24px;
    border-bottom: 1px solid #e8ebf8;
    font-weight: 600;
    color: #5c8ced;
    margin-bottom: 16px;
    padding-bottom: 12px;
    
    i {
      margin-right: 8px;
      font-size: 18px;
    }
  }
  
  .draw-type-content {
    .custom-radio-group {
      display: flex;
      flex-direction: column;
      gap: 12px;
      width: 100%;
      
      .custom-radio {
        margin: 0;
        padding: 12px;
        border-radius: 8px;
        border: 1px solid #e4e9f2;
        transition: all 0.3s ease;
        background: white;
        
        &:hover {
          border-color: #5c8ced;
          box-shadow: 0 2px 8px rgba(92, 140, 237, 0.1);
        }
        
        ::v-deep .el-radio__input {
          display: none;
        }
        
        ::v-deep .el-radio__label {
          padding: 0;
          width: 100%;
        }
        
        .radio-content {
          display: flex;
          align-items: center;
          width: 100%;
          
          i {
            font-size: 20px;
            margin-right: 12px;
            color: #5c8ced;
          }
          
          span {
            font-size: 14px;
            font-weight: 500;
            color: #303133;
          }
        }
      }
      
      ::v-deep .el-radio.is-checked {
        border-color: #5c8ced;
        background: linear-gradient(135deg, #ecf2ff 0%, #e0e9ff 100%);
        
        .radio-content {
          i, span {
            color: #5c8ced;
          }
        }
      }
    }
    
    .type-description {
      margin-top: 16px;
      padding: 12px;
      background: rgba(92, 140, 237, 0.05);
      border-radius: 6px;
      border-left: 3px solid #5c8ced;
      
      .description-item {
        display: flex;
        align-items: center;
        
        i {
          margin-right: 8px;
          color: #5c8ced;
          font-size: 14px;
        }
        
        span {
          font-size: 12px;
          color: #606266;
          line-height: 1.5;
        }
      }
    }
  }
}
.custom-collapse-container {
  border: none;
  
  ::v-deep .el-collapse-item {
    margin-bottom: 12px;
    overflow: hidden;
    border: 1px solid #ebeef5;
    
    &__header {
      height: 48px; /* 降低高度 */
      line-height: 48px;
      padding: 0 16px;
      font-size: 14px;
      font-weight: 600;
      color: #409EFF;
      
      &.is-active {
        border-bottom-color: #dcdfe6;
      }
      
      .el-collapse-item__arrow {
        font-weight: bold;
        color: #409EFF;
      }
    }
    
    &__wrap {
      border: none;
      background: #fff;
    }
    
    &__content {
      padding: 0;
    }
  }
}

.collapse-title-wrapper {
  display: flex;
  align-items: center;
  width: 100%;
  
  .title-icon {
    font-size: 16px; /* 缩小图标 */
    margin-right: 8px; /* 减少间距 */
    color: #409EFF;
  }
  
  .collapse-title {
    flex: 1;
    font-weight: 600;
    font-size: 14px;
  }
  
  .badge-count {
    margin-right: 20px;
  }
}

// 元素列表样式
.element-list-wrapper {
  padding: 10px; /* 减少内边距 */
  max-height: 400px;
  overflow-y: auto;
}

.element-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px; /* 减少内边距 */
  margin-bottom: 8px; /* 减少外边距 */
  border-radius: 6px;
  background: #fafbfc;
  border: 1px solid #ebeef5;
  transition: all 0.3s ease;
  
  &:hover {
    box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.1);
    transform: translateY(-1px);
    border-color: #409EFF;
  }
  
  &.point-item:hover {
    background: rgba(64, 158, 255, 0.03);
  }
  
  &.text-item:hover {
    background: rgba(103, 194, 58, 0.03);
  }
  
  .element-main {
    display: flex;
    align-items: center;
    flex: 1;
    min-width: 0;
  }
  
  .element-marker {
    width: 24px; /* 缩小标记大小 */
    height: 24px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 10px; /* 减少间距 */
    flex-shrink: 0;
    i{
       color: white;
    }
    
    &.text-marker {
      background: linear-gradient(135deg, #67C23A, #85ce61);
      color: white;
      font-size: 12px; /* 缩小图标 */
      
      i {
        font-size: 14px; /* 缩小图标 */
      }
    }
  }
  
  .element-info {
    flex: 1;
    min-width: 0;
    
    .element-name {
      font-size: 13px; /* 调整字体大小 */
      font-weight: 500;
      color: #303133;
      margin-bottom: 2px; /* 减少间距 */
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    .element-id {
      font-size: 11px; /* 调整字体大小 */
      color: #909399;
    }
  }
  
  .element-actions {
    display: flex;
    gap: 6px; /* 减少间距 */
    
    .action-btn {
      width: 26px; /* 缩小按钮 */
      height: 26px;
      
      &.edit-btn {
        background: linear-gradient(135deg, #409EFF, #64b5ff);
        border: none;
        
        i {
          font-size: 12px; /* 缩小图标 */
        }
      }
      
      &.delete-btn {
        background: linear-gradient(135deg, #F56C6C, #f78989);
        border: none;
        
        i {
          font-size: 12px; /* 缩小图标 */
        }
      }
    }
  }
}

// 空状态样式
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 15px; /* 减少内边距 */
  color: #c0c4cc;
  text-align: center;
  
  .empty-icon {
    font-size: 36px; /* 缩小图标 */
    margin-bottom: 10px; /* 减少间距 */
    color: #dcdfe6;
  }
  
  .empty-text {
    margin: 0 0 6px 0; /* 减少间距 */
    font-size: 13px; /* 调整字体大小 */
    color: #909399;
    font-weight: 500;
  }
  
  .empty-tip {
    font-size: 11px; /* 调整字体大小 */
    color: #c0c4cc;
  }
}
</style>